package com.lxmes.hr.service.impl;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcPersonnelInfo;
import com.lxmes.hr.mapper.NcPersonnelInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcWagesAccountMapper;
import com.lxmes.hr.domain.NcWagesAccount;
import com.lxmes.hr.service.INcWagesAccountService;

import javax.validation.Validator;

/**
 * 薪酬基数设置Service业务层处理
 *
 * @author zhangzhiyu
 * @date 2022-11-25
 */
@Service
public class NcWagesAccountServiceImpl implements INcWagesAccountService
{
    @Autowired
    private NcWagesAccountMapper ncWagesAccountMapper;

    @Autowired
    private NcPersonnelInfoMapper ncPersonnelInfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询薪酬基数设置
     *
     * @param id 薪酬基数设置主键
     * @return 薪酬基数设置
     */
    @Override
    public NcWagesAccount selectNcWagesAccountById(Long id)
    {
        return ncWagesAccountMapper.selectNcWagesAccountById(id);
    }

    /**
     * 查询薪酬基数设置列表
     *
     * @param ncWagesAccount 薪酬基数设置
     * @return 薪酬基数设置
     */
    @Override
    public List<NcWagesAccount> selectNcWagesAccountList(NcWagesAccount ncWagesAccount)
    {
        List<NcWagesAccount> ncWagesAccounts = ncWagesAccountMapper.selectNcWagesAccountList(ncWagesAccount);
        return ncWagesAccounts;
    }

    /**
     * 新增薪酬基数设置
     *
     * @param ncWagesAccount 薪酬基数设置
     * @return 结果
     */
    @Override
    public int insertNcWagesAccount(NcWagesAccount ncWagesAccount)
    {
        return ncWagesAccountMapper.insertNcWagesAccount(ncWagesAccount);
    }

    /**
     * 修改薪酬基数设置
     *
     * @param ncWagesAccount 薪酬基数设置
     * @return 结果
     */
    @Override
    public int updateNcWagesAccount(NcWagesAccount ncWagesAccount)
    {
        return ncWagesAccountMapper.updateNcWagesAccount(ncWagesAccount);
    }

    /**
     * 批量删除薪酬基数设置
     *
     * @param ids 需要删除的薪酬基数设置主键
     * @return 结果
     */
    @Override
    public int deleteNcWagesAccountByIds(Long[] ids)
    {
        return ncWagesAccountMapper.deleteNcWagesAccountByIds(ids);
    }

    /**
     * 删除薪酬基数设置信息
     *
     * @param id 薪酬基数设置主键
     * @return 结果
     */
    @Override
    public int deleteNcWagesAccountById(Long id)
    {
        return ncWagesAccountMapper.deleteNcWagesAccountById(id);
    }

    @Override
    public String imporIteminfo(List<NcWagesAccount> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcWagesAccount org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcWagesAccount(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、标准 " + org.getWaName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getWaName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int updateSyncData() {
        List<NcPersonnelInfo> personnelInfos = ncPersonnelInfoMapper.selectNcPersonnelInfoList(null);
        personnelInfos.parallelStream().forEach(p->{
            NcWagesAccount ncWagesAccount = new NcWagesAccount();
            ncWagesAccount.setJobNum(p.getJobNo());
            List<NcWagesAccount> ncWagesAccounts = ncWagesAccountMapper.selectNcWagesAccountList(ncWagesAccount);
            ncWagesAccount.setFactoryCode(p.getFactoryCode());
            ncWagesAccount.setFactoryName(p.getFactoryName());
            ncWagesAccount.setWaName(p.getPName());
            ncWagesAccount.setDepartment(p.getDepartment());
            ncWagesAccount.setDivision(p.getBranch());
            ncWagesAccount.setWaGroup(p.getPGroup());
            ncWagesAccount.setPost(p.getJobs());
            ncWagesAccount.setEntryTime(p.getEntryDate());
            ncWagesAccount.setGwClassification(p.getShift());
            if(StringUtils.isNotNull(p.getEntryDate())){
                Temporal startDate = LocalDate.parse(DateUtils.dateTime(p.getEntryDate()));
                Temporal endDate = LocalDate.parse(DateUtils.getDate());
                long years = ChronoUnit.YEARS.between(startDate, endDate);
                ncWagesAccount.setWorkAge(years+"");
            }
            if(ncWagesAccounts.size()==0){
                ncWagesAccountMapper.insertNcWagesAccount(ncWagesAccount);
            }
            else{
                ncWagesAccountMapper.updateNcWagesAccountByJobNum(ncWagesAccount);
            }
        });

        return 1;
    }

}
